package com.mukefile.action;

import com.mukefile.dto.DownloadFileDto;
import com.mukefile.service.DownloadService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.Map;

/**
 * @author xinke.huang@hand-china.com
 * @version 1.0
 * @date 2021/2/12
 */
@Controller
@Scope("prototype")
public class DownloadAction extends ActionSupport implements ModelDriven<DownloadFileDto>{
    private final DownloadFileDto downloadFileDto = new DownloadFileDto();
    /**
     * 获取session
     */
    private final Map<String, Object> session = ActionContext.getContext().getSession();

    @Override
    public DownloadFileDto getModel() {
        return this.downloadFileDto;
    }
    @Resource
    DownloadService downloadService;
    private InputStream inputStream;

    public InputStream getInputStream() {
        return inputStream;
    }

    public void setInputStream(InputStream inputStream) {
        this.inputStream = inputStream;
    }


    /**
     * 文件下载
     */
    public String downloadFile(){
        try {
            // 获取文件实际全路径
            String actualPath = downloadService.getActualPath(downloadFileDto);
            inputStream = new FileInputStream(new File(actualPath));
            String filename = this.downloadFileDto.getFilename();
            String name = new String(filename.getBytes(), "ISO8859-1");
            this.downloadFileDto.setFilename(name);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return SUCCESS;
    }


}
